home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7487 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  53 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.netins.net!isac!gg
  3. From: gg@isac.hces.com (Greg Goodrich)
  4. Subject: Re: Possible to use #define to define global variable?
  5. Message-ID: <1996Feb26.212847.29837@isac.hces.com>
  6. Organization: Health Care Expert Systems
  7. X-Newsreader: TIN [version 1.2 PL2]
  8. References: <31311b97.1170723@news.csus.edu>
  9. Date: Mon, 26 Feb 1996 21:28:47 GMT
  10.  
  11. Jerry Leong (wleong@sfsu.edu) wrote:
  12. : Hi,
  13. :     I recently need to do programming that restrict the number of
  14. : bytes in a structure. Knowing that it might not possible to turn off
  15. : padding in the structure, I am now thinking if it is possible to use
  16. : #define to define a global variable? That way, I can fix the size
  17. : of my structure without worrying about the padding.
  18.  
  19. :    Here's what I have in mind :
  20.  
  21. :    Normally,
  22. :                        struct MSG{
  23. :                                   char   buffer[3];
  24. :                                  };
  25. :    sizeof(struct MSG) is 4 due the the padding on the structure.
  26.  
  27. :    What if,
  28. :                        #define  buffer char[3];
  29. :   That might work, but than I couldn't assign anything to buffer due
  30. : to the nature of #define.
  31.  
  32. :    Is there anyway to work around this problem?
  33.  
  34. : p/s: I am currently working on UNIX for this program. Thanx in advance
  35.  
  36. :         for your responce.
  37.  
  38. #define's are basically macro substitutions that only exist at compile
  39. time.  The example above will not do you any good, as it will simply
  40. replace every occurrence of "buffer" with "char[3]" which is not what
  41. you want.  I do not believe there is any way to "force" the compiler to
  42. allocate a block for you that doesn't fall on a boundary, and as long as
  43. you don't "hack" in your code, this shouldn't be a problem, as it will
  44. be basically invisible to you as a programmer.  The only thing it will
  45. do is add a little extra memory usage to the program.
  46.  
  47. Greg.
  48. -- 
  49. _______________________________________
  50.     Greg Goodrich - gg@hces.com
  51.     Software Engineer
  52.     PACE Health Management Systems
  53.